package OrderManager;
import Method.Conexcute;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
@WebServlet(urlPatterns = "/Addorder")
public class AddorderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int flag=0;
        request.setCharacterEncoding("utf-8");
        String chnum=null;
        String us_id= request.getParameter("us_id");
        String pr_id=request.getParameter("pr_id");
        String price=null;
        String number=request.getParameter("number");
        String date=request.getParameter("date");
        try {
            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;
            Class.forName("org.h2.Driver");
            /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
            String url = "jdbc:h2:~/test5";
            con = DriverManager.getConnection(url, "sa", "sa");
            stmt = con.createStatement();
            String sql =  "select * from product where pr_id='"+pr_id+"' ";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
               if(Integer.valueOf(rs.getString("number"))<Integer.valueOf(number)){
                   flag=1;
               }
               else{
                   price=rs.getString("price");
                   price=String.valueOf(Integer.valueOf(number)*Integer.valueOf(price));
                    chnum=String.valueOf(Integer.valueOf(rs.getString("number"))-Integer.valueOf(number));
               }
            }
            rs.close();
            stmt.close();
            con.close();
        }catch (Exception e){e.getMessage();}
        if(flag==0) {
            System.out.println(flag);
            Conexcute conexcute = new Conexcute();
            if(conexcute.execute("update product set number='"+chnum+"' where pr_id='"+pr_id+"'")) {
                if (conexcute.execute("insert into `rder` (`us_id`,`pr_id`,`price`,`number`,`date`) values ( '" + us_id + "', '" + pr_id + "','" + price + "','" + number + "','" + date + "');")) {
                    response.setContentType("text/html");
                    response.setCharacterEncoding("UTF-8");
                    response.getWriter().println("数据添加成功");
                    response.sendRedirect("OrderManager/order.jsp");
                }
            }
        }else{response.sendRedirect("OrderManager/order_insert.jsp?str=flag");}
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
